package com.biz.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.biz.model.common.SessionInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*************************************************************************
 * 文件名称 ：ActionSessionFilter.java                   
 * 描述说明 ：
 * 
 * 创建信息 : create by 刘佳佳 on 上午11:09:37
 * 修订信息 : modify by ( ) on (date) for ( )
 * 
 * 版权信息 : Copyright (c) 2015 扬州北斗科技有限公司 
 **************************************************************************/

public class ActionSessionFilter implements Filter{

	protected static Logger logger = LoggerFactory.getLogger(ActionSessionFilter.class);


	private List<String> list = new ArrayList<String>();
	private String actions = "";
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// 初始化需要拦截的文件夹
				String uninclude = filterConfig.getInitParameter("unincludeAction");
				actions = uninclude;
				if (!StringUtils.isBlank(uninclude)) {
					StringTokenizer st = new StringTokenizer(uninclude, ",");
					list.clear();
					while (st.hasMoreTokens()) {
						list.add(st.nextToken());
					}
				}
				
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String servletPath = request.getServletPath();
		SystemSession.setUserSession((SessionInfo) request.getSession().getAttribute("sessionInfo"));
		logger.info("进入session过滤器->访问路径为[" + servletPath + "]");
//		for (String action : list) {
//			if (servletPath.indexOf(action) == -1) {// 需要过滤
//				logger.info("进入session过滤器->访问路径为[" + servletPath + "]");
//				String filterPath = servletPath.substring(servletPath.lastIndexOf("/")+1,servletPath.indexOf(".action"));
//				if (request.getSession().getAttribute("sessionInfo") == null&&!filterPath.equals("logout")&&!filterPath.equals("login")) {
////				if (request.getSession().getAttribute("sessionInfo") == null&&!action.equals("logout")&&!action.equals("login")) {
//					request.setAttribute("msg", "您还没有登录或登录已超时，请重新登录，然后再刷新本功能！");
//					request.getRequestDispatcher("/jsp/main/noSession.jsp").forward(request, response);
//					return;
//				}
//				break;
//			}
//		}
		
		logger.info("进入session过滤器->访问路径为[" + servletPath + "]");
		String filterPath = servletPath.substring(servletPath.lastIndexOf("/")+1,servletPath.indexOf(".action"));
		if (!actions.contains(filterPath)) {// 需要过滤
			if (request.getSession().getAttribute("sessionInfo") == null) {
				request.setAttribute("msg", "您还没有登录或登录已超时，请重新登录，然后再刷新本功能！");
				request.getRequestDispatcher("/jsp/main/noSession.jsp").forward(request, response);
				return;
			}
		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

}
